package cn.getech.data.development.controller;

import cn.getech.data.development.model.dto.CMClusterAnalyDto;
import cn.getech.data.development.model.dto.CMClusterDto;
import cn.getech.data.development.model.dto.JobRunTOPDto;
import cn.getech.data.development.model.dto.QueueStaticUsedHourDto;
import cn.getech.data.development.model.qo.CMClusterSearchQO;
import cn.getech.data.development.model.qo.CMClusterSearchTimeQO;
import cn.getech.data.development.model.vo.*;
import cn.getech.data.development.service.BigScreenService;
import cn.getech.data.intelligence.common.utils.PojoUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bdp/cm/bigscreen")
public class BigScreenController {

    @Autowired
    private BigScreenService bigScreenService;

    @ApiOperation("获取大屏重点关注总计")
    @GetMapping("/focusSum")
    @SysLog(value="获取大屏重点关注总计",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R focusSum() {
        CMClusterDto dto = bigScreenService.focusSum();
        List<CMClusterAnalyDto> rest = bigScreenService.clusterLastCPU();
        CMClusterSumVO cmClusterSumVO = new CMClusterSumVO();
        if(null != dto){
            BeanUtils.copyProperties(dto,cmClusterSumVO);
        }
        if(CollectionUtil.isNotEmpty(rest)){
            cmClusterSumVO.setCpuPercentAcross(rest.get(0).getY());
        }
        return R.okWithData(cmClusterSumVO);
    }


    @ApiOperation("获取大屏重点关注信息")
    @PostMapping("/focusInfo")
    @SysLog(value="获取大屏重点关注信息",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R focusInfo(@RequestBody CMClusterSearchQO cmClusterSearchQO) {
        List<CMClusterDto> rest = bigScreenService.focusInfo(cmClusterSearchQO,1);
        if(CollectionUtil.isNotEmpty(rest)){
            return R.okWithData(PojoUtils.listConvert(CMClusterVO.class,rest.stream().sorted(Comparator.comparing(CMClusterDto :: getCpuPercentAcross).reversed()).collect(Collectors.toList())));
        }
        return R.okWithData(null);
    }


    @ApiOperation("获取大屏集群数据-hdfs容量")
    @PostMapping("/clusterHdfsInfo")
    @SysLog(value="获取大屏集群数据-hdfs容量",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R clusterHdfsInfo(@RequestBody CMClusterSearchTimeQO cmClusterSearchTimeQO) {
        CMClusterHDFSVO rest = bigScreenService.clusterHdfsInfo(cmClusterSearchTimeQO);
        return R.okWithData(rest);
    }

    @ApiOperation("获取大屏集群数据-网络io")
    @PostMapping("/clusterNetIO")
    @SysLog(value="获取大屏集群数据-网络io",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R clusterNetIO(@RequestBody CMClusterSearchTimeQO cmClusterSearchTimeQO) {
        CMClusterNetIOVO rest = bigScreenService.clusterNetIO(cmClusterSearchTimeQO);
        return R.okWithData(rest);
    }

    @ApiOperation("获取大屏集群数据-CPU")
    @PostMapping("/clusterCPU")
    @SysLog(value="获取大屏集群数据-CPU",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R clusterCPU(@RequestBody CMClusterSearchTimeQO cmClusterSearchTimeQO) {
        List<CMClusterAnalyDto> rest = bigScreenService.clusterCPU(cmClusterSearchTimeQO);
        return R.okWithData(bigScreenService.changeTimeData(rest));
    }

    @ApiOperation("获取队列的实时使用大小")
    @GetMapping("/lastQueueMemory")
    @SysLog(value="获取队列的实时使用大小",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R lastQueueMemory() {
        List<CMClusterAnalyDto> rest = bigScreenService.lastQueueMemory();
        return R.okWithData(PojoUtils.listConvert(CMClusterQueueVO.class,rest));
    }

    @ApiOperation("获取历史的调度队列使用数据集合")
    @PostMapping("/queueMemoryHistory")
    @SysLog(value="获取历史的调度队列使用数据集合",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R queueMemoryHistory(@RequestBody CMClusterSearchTimeQO cmClusterSearchTimeQO) {
        List<QueueStaticUsedHourDto> rest = bigScreenService.queueMemoryHistory(cmClusterSearchTimeQO);
        QueueStaticUsedHourVO dataCharts = bigScreenService.queueMemoryHistoryCharts(rest,cmClusterSearchTimeQO);
        return R.okWithData(dataCharts);
    }

    @ApiOperation("获取任务执行时长排行TOP20")
    @PostMapping("/runTimeTop")
    @SysLog(value="获取任务执行时长排行TOP20",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R runTimeTop(@RequestBody CMClusterSearchTimeQO cmClusterSearchTimeQO) {
        List<JobRunTOPDto> rest = bigScreenService.runTimeTop(cmClusterSearchTimeQO);
        return R.okWithData(PojoUtils.listConvert(JobRunTOPVO.class,rest));
    }

    @ApiOperation("获取出错次数排行TOP20")
    @PostMapping("/runFailNumTop")
    @SysLog(value="获取出错次数排行TOP20",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R runFailNumTop(@RequestBody CMClusterSearchTimeQO cmClusterSearchTimeQO) {
        List<JobRunTOPDto> rest = bigScreenService.runFailNumTop(cmClusterSearchTimeQO);
        return R.okWithData(PojoUtils.listConvert(JobRunTOPVO.class,rest));
    }


}

